import router, { asyncRoutes, constantRoutes } from '@/router'
export default {
    namespaced: true,
    state: {
        routes: [],
        roles: {}
    },
    mutations: {
        setRoutes(state, payload) {
            state.routes = payload
        },
        setRoles(state, payload) {
            state.roles = payload
        }
    },
    actions: {
        filterRoutes(context, roles) {
            // 过滤之后的路由
            const routes = asyncRoutes.filter(item => {
                return roles.menus.includes(item.meta.id)
            })
            // 动态添加路由
            router.addRoutes([...routes, 
                { path: '*', redirect: '/404', hidden: true }])
            context.commit('setRoutes', [...constantRoutes, ...routes])
            context.commit('setRoles', roles)
        }
    }
}
